package sisi.agents;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * AgentController.java - Controller class for the <i>Agents database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 03/12/2013
 * 
 */
public class AgentController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Agent[] get(String cerca) {
		List<Agent> lista = getList(cerca);
			return lista.toArray(new Agent[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Agent> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.companyname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Agent as c " + cWhere 
					+ " order by c.companyname ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Agent> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	public Agent getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Agent agentx = em.find(Agent.class, id);
			return agentx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Agent add(Agent agent) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(agent);
			em.getTransaction().commit();
			agent = refresh(agent);
		} finally {
			em.close();
			return agent;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Agent agent) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Agent agentx = em.find(Agent.class, agent.getAgentid());
			agentx.setAddress(agent.getAddress());
			agentx.setCountry(agent.getCountry());
			agentx.setAgentid(agent.getAgentid());
			agentx.setAgentvalut(agent.getAgentvalut());
			agentx.setCity(agent.getCity());
			agentx.setCompanyname(agent.getCompanyname());
			agentx.setContactname(agent.getContactname());
			agentx.setContacttitle(agent.getContacttitle());
			agentx.setEmail(agent.getEmail());
			agentx.setFax(agent.getFax());
			agentx.setGsm(agent.getGsm());
			agentx.setHomepage(agent.getHomepage());
			agentx.setNotes(agent.getNotes());
			agentx.setPhone(agent.getPhone());
			agentx.setPortid(agent.getPortid());
			agentx.setPostalcode(agent.getPostalcode());
			agentx.setRegion(agent.getRegion());
			agentx.setReq(agent.getReq());
			agentx.setReservednotes(agent.getReservednotes());
			agentx.setDtup(agent.getDtup());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Agent refresh(Agent agent) {

		Agent agentx = agent;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			agentx = em.find(Agent.class, agent.getAgentid());
			em.flush();
			em.refresh(agentx);
		} finally {
			em.close();
			return agentx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Agent agent) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Agent agentx = em.find(Agent.class, agent.getAgentid());
			em.remove(agentx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//
// fine
